using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.OleDb;




namespace SqlToolkit.Oracle.Schema
{
    public class SqlDbView : SqlBase
    {

        internal SqlDbView() { }
        internal SqlDbView(OracleDB conn) { _conn = conn; }
        #region VIEW_NAME
        string view_name = string.Empty;
        public string ViewName
        {
            get { return view_name; }
            //set{if(value != view_name) view_name = value;}
        }
        #endregion
        #region Columns
        SqlDbViewFieldCollection col = null;
        public SqlDbViewFieldCollection Columns
        {
            get
            {
                if (null == col)
                    col = SqlSchema.ViewColumns(_conn, this.view_name);

                return col;
            }
        }
        #endregion
        #region +Attach Columns
        internal static SqlDbView Attach(SqlDbView view, SqlDbViewFieldCollection columns)
        {
            view.col = columns;
            return view;

        }
        #endregion
        #region +Build
        internal static SqlDbView Build(OracleDB conn, System.Data.IDataReader r)
        {
            SqlDbView ret = new SqlDbView(conn);

            if (r.IsDBNull(0))
                ret.view_name = "ERROR INVALID RESULT SET";
            else
                ret.view_name = r.GetString(0);

            return ret;
        }
        #endregion
        #region +BuildCollection
        internal static SqlDbViewCollection BuildCollection(OracleDB conn, System.Data.IDataReader r)
        {
            SqlDbViewCollection tbls = new SqlDbViewCollection();

            while (r.Read())
                tbls.Add(SqlDbView.Build(conn, r));

            return tbls;
        }
        #endregion

        public string ViewNameFriendly()
        {
            return FriendlyName(this.ViewName);
        }

        //public DataTable GetData()
        //{
        //    DataTable ret = null;

        //    if (null != this._conn)
        //    {
        //        string sql = SqlGenerator.CreateQuery(this, null);
        //        try
        //        {
        //            ret = this._conn.DbAccessor.ExecuteSqlText.ExecuteDataTable(sql);
        //        }
        //        catch (Exception ex)
        //        {
        //            System.Diagnostics.Debug.WriteLine(ex.Message);
        //            ret = null;
        //        }
        //    }

        //    return ret;
        //}

        public int GetColumnIndex(string columnName)
        {
            if (null == this.Columns) { return -1; }

            for (int i = 0; i < this.Columns.Count; i++)
            {
                if (this.Columns[i].ColumnName.ToUpper().Trim() == columnName.ToUpper().Trim())
                {
                    return i;
                }
            }

            return -1;
        }

    }
}
